package com.fireghost.jdt.mainActivity;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.fireghost.jdt.R;
import com.fireghost.jdt.database.JournalDeTravailDbAdapter;
import com.fireghost.jdt.database.TypeDbEntry;

public class SpinnerTypesAdapter extends BaseAdapter implements SpinnerAdapter {
	
	private ArrayList<TypeDbEntry> a_oTypes;
	private Context oContext;
	
	
	public SpinnerTypesAdapter(Context context) {
		a_oTypes = new ArrayList<TypeDbEntry>();
		oContext = context;
	}
	
	public int getCount() {
		return a_oTypes.size();
	}

	public Object getItem(int p_iPosition) {
		return a_oTypes.get(p_iPosition);
	}

	public long getItemId(int p_iPosition) {
		return a_oTypes.get(p_iPosition).getTypeID();
	}

	public View getView(int p_iPosition, View p_oConvertView, ViewGroup p_oParentViewGroup) {
		LinearLayout oLayoutItem;
		
		// Reusing the old layout (gain of speed)
		if (p_oConvertView == null) {
			oLayoutItem = (LinearLayout)LayoutInflater.from(oContext).inflate(R.layout.spinner_types_item, p_oParentViewGroup, false);
		}
		else {
			oLayoutItem = (LinearLayout)p_oConvertView;
		}
		
		// Get and set the textview
		TextView oTxtTypeTitle = (TextView)oLayoutItem.findViewById(R.id.oTxtSpinnerTypeTitle);
		oTxtTypeTitle.setText(a_oTypes.get(p_iPosition).getTypeDescription());
		
		return oLayoutItem;
	}
	
	public void populate(JournalDeTravailDbAdapter p_oJdtDb) {
		a_oTypes = p_oJdtDb.getTypes();
		
		this.notifyDataSetChanged();
	}

}
